import request from '@/api/axios'
import {AxiosPromise} from 'axios'
import {DataRecordList, DataRes} from '@/api/config'
import {BasePhysicalExamination} from './index'
import {Operation as Opr} from '@/components/dTable/dTable'

/**
 * @description: 主要用药情况
 * @return {*}
 */
export interface MainMedication extends BasePhysicalExamination {
  drugName?: string // 药品名称
  drugUseMethod?: number // 用法 - 字典
  frequency?: number // 用药频率 - 字典
  dose?: string // 用药剂量
  doseUnit?: number // 用药剂量单位 - 字典
  drugUseTime?: number // 用药时间
  drugTimeUnit?: number //用药时间单位 - 字典
  drugCompliable?: number // 服药依从性 - 字典
  drugUseMethodName?: string //用法
  doseName?: string // 用药剂量
  drugUseTimeName?: string //用药时间
  drugCompliableName?: string //服药依从性
  operation?: Array<Opr>
}

/**
 * @description: 保存主要用药情况
 * @param {MainMedication} data
 * @return {*}
 */
export const saveMainMedication = (data: Array<MainMedication>): AxiosPromise<DataRes<undefined>> => {
  return request({
    url: '/api/medicationSituation/save',
    method: 'post',
    data
  })
}

/**
 * @description: 根据体检登记表id获取主要用药情况数据
 * @param {string} checkupRegisterId  //  checkupRegisterId 体检登记表id
 * @return {*}
 */
export const getMainMedication = (checkupRegisterId: string): AxiosPromise<DataRes<Array<MainMedication>>> => {
  return request({
    url: `/api/medicationSituation/get/${checkupRegisterId}`,
    method: 'get'
  })
}

/**
 * @description: 拒检主要用药情况
 * @param {BasePhysicalExamination} data
 * @return {*}
 */
export const rejectMainMedication = (data: Array<BasePhysicalExamination>): AxiosPromise<DataRes<undefined>> => {
  return request({
    url: '/api/medicationSituation/reject',
    method: 'post',
    data
  })
}

// 药品库分页查询
export interface DrugStoreParams {
  drugCode?: string // 药品代码
  drugName?: string // 药品名称
  page?: number
  limit?: number
}
// 药品库列表
export interface DrugStoreList {
  id?: string
  drugCode?: string // 药品代码
  drugName?: string // 药品名称
  pinyinCode?: string | null // 拼音码
  drugUseMethod?: number | null // 用法 - 字典
  frequency?: number | null // 用药频率 - 字典
  dose?: string | null // 用药剂量
  doseUnit?: number // 用药剂量单位 - 字典
  drugUseTime?: number // 用药时间
  drugTimeUnit?: number //用药时间单位 - 字典
  drugCompliable?: number | null // 服药依从性 - 字典
}

/**
 * @description: 药品库分页查询
 * @param {DrugStoreParams} data
 * @return {*}
 */
export const getDrugStore = (params: DrugStoreParams): AxiosPromise<DataRecordList<DrugStoreList>> => {
  return request({
    url: '/api/drugStore/page',
    method: 'get',
    params
  })
}

/**
 * @description: 保存药品库信息
 * @param {DrugStoreList} data
 * @return {*}
 */
export const saveDrugStore = (data: DrugStoreList): AxiosPromise<DataRes<undefined>> => {
  return request({
    url: '/api/drugStore/save',
    method: 'post',
    data
  })
}

/**
 * @description: 删除药品库信息
 * @param {string} id 药品id
 * @return {*}
 */
export const deleteDrugStore = (id: string): AxiosPromise<DataRes<undefined>> => {
  return request({
    url: `/api/drugStore/remove/${id}`,
    method: 'post'
  })
}

/**
 * @description: 搜索药品库数据
 * @param {object} params
 * @return {*}
 */
export const getDrugStoreByName = (params: {drugName: string}): AxiosPromise<DataRes<Array<DrugStoreList>>> => {
  return request({
    url: '/api/drugStore/list',
    method: 'get',
    params
  })
}

/**
 * @description: 获取名称大写拼音码
 * @param {object} params
 * @return {*}
 */
export const getUpperCase = (params: {drugName: string}): AxiosPromise<DataRes<string>> => {
  return request({
    url: '/api/drugStore/getUpperCase',
    method: 'get',
    params
  })
}
